%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Reproducing Table 3 of the paper:
%Reis, Ricardo (2006) "Inattentive Producers," Review of Economic Studies.
%Written by: Ricardo Reis
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clc
clear

%STEP 1: SET PARAMETERS
alpha=0.11;
lambda=0.25;
par1=0.3866896; %nominal income ar coefficient
par0=0.0105235; %nominal income intercept
upper=100;

%STEP 2: GET THE MA REPRESENTATION FOR THE DIFFERENT SERIES
InPro_Table3a

%STEP 3: LOAD DATA
%1st column has index, 2nd has dates, 3rd has m, 4th has p, 5th has a, 6th has g_m, 7th has pi, 8th has g_a
load InPro_data.txt
index=InPro_data(:,1);
n=length(index);
y=InPro_data(:,3)-InPro_data(:,4);
a=InPro_data(:,5);
g_m=InPro_data(:,6);
pi=InPro_data(:,7);
p_index=index(2:n,1);
g_a=InPro_data(:,8);

%STEP 4: INNOVATIONS
tech_innovation=g_a-mean(g_a);
tech_innovation=tech_innovation(2:n,1);
m_innovation=g_m(2:n,1)-par0-par1*g_m(1:n-1,1);    %read off stata the fit process for g_m

%STEP 5: RUN IN REAL TIME
start_m=zeros(upper-1,1);
start_m(upper,1)=(1/gamma(1,1))*pi(1,1);
temp_m=[start_m; m_innovation];
start_tech=zeros(upper,1);
temp_tech=[start_tech; tech_innovation];
for j=1:n-1
    pred_pi_m(j,1)=gamma'*flipud(temp_m(j:upper+j,1));
    pred_pi_tech(j,1)=csi'*flipud(temp_tech(j:upper+j,1));
    pred_pi(j,1)=pred_pi_m(j,1)+pred_pi_tech(j,1);
end
%Constant from both components
pred_cons=par0/(1-par1)-mean(g_a);
pred_pi=pred_pi+pred_cons*ones(n-1,1);

%STEP 5: ANALYSE FIT FROM 1960 ONWARDS
%Adjusting dates
pred_pi=pred_pi(24:n-1,1);
p_index=p_index(24:n-1,1);
pi=pi(25:n,1);
g_m=g_m(25:n,1);
g_a=g_a(25:n,1);
t=length(pi);
% theoretical moments
legend=['     st dev','     acorr','    corr_m','  corr_m_lead',' corr_m_lag',' corr_a',' corr_a_lead',' corr_a_lag']
mom1=std(pred_pi);
mom2=corrcoef(pred_pi(2:t),pred_pi(1:t-1));
mom2=mom2(1,2);
mom3=corrcoef(pred_pi,g_m);
mom3=mom3(1,2);
mom4=corrcoef(pred_pi(2:t),g_m(1:t-1));
mom4=mom4(1,2);
mom5=corrcoef(pred_pi(1:t-1),g_m(2:t));
mom5=mom5(1,2);
mom6=corrcoef(pred_pi,g_a);
mom6=mom6(1,2);
mom7=corrcoef(pred_pi(2:t),g_a(1:t-1));
mom7=mom7(1,2);
mom8=corrcoef(pred_pi(1:t-1),g_a(2:t));
mom8=mom8(1,2);
Theory=[mom1 mom2 mom3 mom4 mom5 mom6 mom7 mom8]
mom1=std(pi);
mom2=corrcoef(pi(2:t),pi(1:t-1));
mom2=mom2(1,2);
mom3=corrcoef(pi,g_m);
mom3=mom3(1,2);
mom4=corrcoef(pi(2:t),g_m(1:t-1));
mom4=mom4(1,2);
mom5=corrcoef(pi(1:t-1),g_m(2:t));
mom5=mom5(1,2);
mom6=corrcoef(pi,g_a);
mom6=mom6(1,2);
mom7=corrcoef(pi(2:t),g_a(1:t-1));
mom7=mom7(1,2);
mom8=corrcoef(pi(1:t-1),g_a(2:t));
mom8=mom8(1,2);
Data=[mom1 mom2 mom3 mom4 mom5 mom6 mom7 mom8]
clear mom1 mom2 mom3 mom4 mom5 mom6 mom7 mom8

%Measurement error adjustment
temp=Data(1,1)^2-Theory(1,1)^2;
disp('Acorr theory with measurement error ')
disp(Theory(1,2)/(1+temp/Theory(1,1)^2))